package cn.b.sky.admin.service.impl;

import cn.b.sky.admin.dao.FunctionGroupDao;
import cn.b.sky.admin.model.BatchJobReport;
import cn.b.sky.admin.model.FunctionGroup;
import cn.b.sky.admin.model.Page;
import cn.b.sky.admin.model.condition.FunctionGroupCondition;
import cn.b.sky.admin.service.FunctionGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 
 * <strong>Title : FunctionGroupServiceImpl.java<br>
 * </strong> <strong>Package : cn.b.sky.admin.service.impl<br>
 * </strong> <strong>Description : </strong>功能组操作业务类实现<br>
 * <strong>Create on : 2016-6-7 上午11:36:02<br>
 * </strong>
 * <p>
 * 
 * @author JunLin.Yang<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人|修改日期|修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("functionGroupService")
public class FunctionGroupServiceImpl implements FunctionGroupService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(FunctionGroupServiceImpl.class);

	@Autowired
	FunctionGroupDao functionGroupDao;

	@Override
	public Page selectFunctionGroupsPageByCondition(
			FunctionGroupCondition condition) {
		condition.setData(this.functionGroupDao
				.selectFunctionGroupsByCondition(condition));
		condition.setTotalCount(this.functionGroupDao
				.getFunctionGroupsCountByCondition(condition));
		return condition;
	}

	@Override
	public FunctionGroup getFunctionGroupByPK(String functionGroupCode) {
		return this.functionGroupDao.getFunctionGroupByPK(functionGroupCode);
	}

	@Override
	public String insertOrUpdateFunctionGroup(FunctionGroup data) {
		FunctionGroup dbData = this.functionGroupDao.getFunctionGroupByPK(data
				.getFunctionGroupCode());
		if (null != dbData) {
			data.setCreateDateTime(null);
			data.setCreateUser(null);
			this.functionGroupDao.updateFunctionGroup(data);
		} else {
			this.functionGroupDao.insertFunctionGroup(data);
		}
		return data.getFunctionGroupCode();
	}

	@Override
	public BatchJobReport deleteFunctionGroupByPK(String functionGroupCode) {
		String cause = "";
		Boolean success = true;
		try {
			this.functionGroupDao.deleteFunctionGroupByPK(functionGroupCode);
		} catch (Exception e) {
			LOGGER.error("{}", e);
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			cause = e.getMessage();
			success = false;
		}
		return new BatchJobReport(functionGroupCode, cause, success, "zh");
	}

	@Override
	public List<FunctionGroup> getAllFunctionGroups() {
		return functionGroupDao.getAllFunctionGroups();
	}

	@Override
	public List<FunctionGroup> getFunctionGroupsByCodes(String[] groupCodes) {
		if (null == groupCodes) {
			groupCodes = new String[] {};
		}
		return functionGroupDao.getFunctionGroupsByCodes(groupCodes);
	}

}
